package com.accenture.jproxy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;

import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;

import com.google.common.collect.ImmutableMap;

/**
 * 实现代理配置
 *
 */
@Configuration
public class ProxyServletConfiguration {

    // 读取配置中代理目标地址
    @Value("${proxy.target_url}")
    private String targetUrl;

    @Value("${proxy.url}")
    private String proxyUrl;
    
    @Bean
    public Servlet createProxyServlet() {
    	List<String> blacklist = new ArrayList<>();
    	blacklist.add("proxy.js");
    	blacklist.add("log-event/write");
        return new ExProxyServlet(blacklist,proxyUrl);
//        return new ProxyServlet();
    }
    
    /**
     * Proxy
     * 
     * @return
     */
    @Bean
    public ServletRegistrationBean<Servlet> proxyServletRegistration2() {
    	
        ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<Servlet>(createProxyServlet(),"/*");
        Map<String, String> params = ImmutableMap.of("targetUri", targetUrl);
        registrationBean.setInitParameters(params);
        return registrationBean;
        
    }
    /**
     * Filter
     * 
     * @return
     */
    @Bean
    public FilterRegistrationBean<ResponseFilter> someFilterRegistration()
    {
        FilterRegistrationBean<ResponseFilter> registration = new FilterRegistrationBean<ResponseFilter>();
        registration.setFilter(new ResponseFilter());
        registration.addUrlPatterns("/*");
        registration.setName("responseFilter");
        return registration;
    }
}